package com.cjg.Queue;

import java.util.concurrent.*;

public class demo02 {

    public static void main(String[] args) {

        //电脑的线程数
        System.out.println(Runtime.getRuntime().availableProcessors());
        ExecutorService executor = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors(),
                1,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
                );
        /**
         * new ThreadPoolExecutor.AbortPolicy()   不处理爆出异常
         * new ThreadPoolExecutor.CallerRunsPolicy()  哪来回哪条线程 就是返回main线程
         * new ThreadPoolExecutor.DiscardPolicy() 丢弃掉任务 不爆出异常
         * new ThreadPoolExecutor.DiscardOldestPolicy() 尝试与最开始的线程竞争
         */

        try {
            for (int i = 0; i < 90; i++) {
                //使用线程池后使用新方法创建线程
                executor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"::::::OK");
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executor.shutdown();
        }


    }
}
